package com.wealth.mycenter.web.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.wealth.commodity.entity.Comment;
import com.wealth.commodity.entity.Goods;
import com.wealth.commodity.service.ICommentService;
import com.wealth.commodity.service.IGoodsService;
import com.wealth.coupon.service.ICouponRecordService;
import com.wealth.lottery.entity.LotteryActivity;
import com.wealth.lottery.entity.LotteryRecord;
import com.wealth.lottery.service.ILotteryActivityService;
import com.wealth.lottery.service.ILotteryRecordService;
import com.wealth.member.entity.Address;
import com.wealth.member.entity.Member;
import com.wealth.member.service.IAddressService;
import com.wealth.order.entity.Order;
import com.wealth.order.service.IOrderService;
import com.wealth.system.entity.Corporation;
import com.wealth.system.web.controller.BaseController;

import iaas.enums.StatusEnum;
import iaas.enums.YesOrNoEnum;
import iaas.utils.RedirectMessage;
import iaas.utils.ResultObject;
@Controller
@RequestMapping(value="/manage/myCenter")
public class MyCenterController extends BaseController{
	Logger logger = Logger.getLogger(MyCenterController.class);
	@Autowired
	private ILotteryRecordService iLotteryRecordService;
	@Autowired
	private ICouponRecordService couponRecordService;
	@Autowired
	private IOrderService iOrderService;
	@Autowired
	private IAddressService addressService;
	@Autowired
	private ILotteryActivityService activityService;
	@Autowired
	private IGoodsService goodsService;
	@Autowired
	private ICommentService commentService;
 	@RequestMapping(value="/myRecord")
	public String toMyLotteryRecord(Model model){
		try {
			List<LotteryActivity> activityList = activityService.getByStatusNot();
			model.addAttribute("activityList", activityList);
			model.addAttribute("flag", "myrecord");
			return "center/lotteryRecord";
		} catch (Exception e) {
			logger.error(e);
			return "system/500";
		}
	}
 	@RequestMapping(value="/myRecordData/{lotteryid}")
 	@ResponseBody
	public ResultObject MyLotteryRecordData(@RequestParam(value="pageNumber",defaultValue="1") Integer pageNumber,
			@RequestParam(value="pageSize",defaultValue="5") Integer pageSize,@PathVariable Long lotteryid){
		try {
			 ResultObject result = iLotteryRecordService.getLotteryRecordByMemberIdPage(pageSize, pageNumber, memberid,lotteryid);
			 return  result;
		} catch (Exception e) {
			logger.error(e);
			return  new ResultObject("111", "加载失败", null);
		}
	}
	@RequestMapping(value="/mycard")
	public String toMyCard(Model model){
		try {
			return "center/myCard";
		} catch (Exception e) {
			logger.error(e);
			return "system/500";
		}
	}
	/**消费记录*/
	@RequestMapping(value="/shoprecord")
	public String toMyShopRecord(Model model){
		try {
			model.addAttribute("flag", "shoprecord");
			return "center/shopRecord";
		} catch (Exception e) {
			logger.error(e);
			return "system/500";
		}
	}
	/**消费记录数据*/
	@RequestMapping(value="/shopData")
	@ResponseBody
	public ResultObject getMyRecordData(@RequestParam(value="pageNumber",defaultValue="1") Integer pageNumber,
			@RequestParam(value="pageSize",defaultValue="5") Integer pageSize
			){
		try {
			return iOrderService.ajaxConsumptionPage(memberid, YesOrNoEnum.YES.getValue(), pageNumber,pageSize);
		} catch (Exception e) {
			return null;
		}
	}
	/**我的订单*/
	@RequestMapping(value="/myOrder")
	public String toMyorder(Model model){
		model.addAttribute("flag", "myorder");
		return "center/myOrder";
	}
	/**我的评价*/
	@RequestMapping(value="/mycomment")
	public String toMyComment(Model model){
		model.addAttribute("flag", "mycomment");
		return "center/myComment";
	}
	/**我的订单数据*/
	@RequestMapping(value="/orderData")
	@ResponseBody
	public ResultObject getMyOrderData(@RequestParam(value="pageNumber",defaultValue="1") Integer pageNumber,
			@RequestParam(value="pageSize",defaultValue="5") Integer pageSize,@RequestParam(value="status",defaultValue="all") String status
			){
		try {
			Integer order_status = null;
			if("all".equals(status)){
				order_status=0;
			}else{
				order_status =Integer.valueOf(status);
			}
			return iOrderService.ajaxOrderPage(memberid, order_status, pageNumber,pageSize);
		} catch (Exception e) {
			return null;
		}
	}
	/**得到我的订单详情*/
	@RequestMapping(value="/orderInfo/{id}")
	public String getOrderInfo(@PathVariable Long id,Model model){
		try {
			Order order = iOrderService.get(id);
			model.addAttribute("order", order);
			model.addAttribute("flag", "myorder");
			return  "center/orderInfo";
		} catch (Exception e) {
			logger.error(e);
			return "system/500";
		}
	}
	/**我的地址*/
	@RequestMapping(value="/myAddress")
	public String myAddress(Model model){
		try {
			model.addAttribute("flag", "myaddress");
			return  "center/myAddress";
		} catch (Exception e) {
			logger.error(e);
			return "system/500";
		}
	}
	/**请求得到我的地址数据*/
	@RequestMapping(value="/addressData")
	@ResponseBody
	public ResultObject getAddressData(@RequestParam(value="pageNumber",defaultValue="1") Integer pageNumber,
			@RequestParam(value="pageSize",defaultValue="5") Integer pageSize){
		try {
			return addressService.getAddressPage(memberid, pageSize, pageNumber);
		} catch (Exception e) {
			return new ResultObject("111", "加载失败", null);
		}
	}
	
	/**新增地址页面*/
	@RequestMapping(value="/create")
	public String  toAddressCreate(Model model){
		model.addAttribute("todo", 0);
		model.addAttribute("flag", "myaddress");
		return "center/addressupdate";
	}
	/**编辑地址页面*/
	@RequestMapping(value="/update/{id}")
	public String  toAddressUpdate(@PathVariable Long id,Model model){
		try {
			model.addAttribute("flag", "myaddress");
			Address address=addressService.get(id);
			model.addAttribute("address", address);
			model.addAttribute("todo", 1);
			return "center/addressupdate";
		} catch (Exception e) {
			logger.error(e);
			return "system/500";
		}
	}
	
	/**保存地址*/
	@RequestMapping(value="/saveaddress/{type}")
	public String saveAddress(Address address,@PathVariable Integer type,RedirectAttributes redirectAttributes){
		try {
			/**如果把该地址设置成了默认地址，则把另一把默认地址变成非默认地址*/
			if(address.getIsDefault()==1){
				Address deAddress = addressService.getAddressDefault(memberid);
				if(deAddress!=null){
					deAddress.setIsDefault(YesOrNoEnum.NO.getValue());
					addressService.save(deAddress);
				}
			}
			String msg = "";
			if(type==0){
				address.setCorp(new Corporation(corpid));
				address.setCreateTime(new Date());
				address.setStatus(StatusEnum.NORMAL.getValue());
				address.setMember(new Member(memberid));
				addressService.saveAddress(address);
				msg ="新增成功";
			}else{
				addressService.saveAddress(address);
				msg = "修改成功";
			}
			redirectAttributes.addFlashAttribute("message", RedirectMessage.setShowTipSuccess(msg));
			return "redirect:/manage/myCenter/myAddress";
		} catch (Exception e) {
			return "system/500";
		}
	}
	/**删除地址*/
	@RequestMapping(value="/deleteaddress/{id}")
	public String deleteAddress(@PathVariable Long id,RedirectAttributes redirectAttributes){
		try {
			addressService.deleteAddress(id);
			redirectAttributes.addFlashAttribute("message", RedirectMessage.setShowTipSuccess("删除成功"));
			return "redirect:/manage/myCenter/myAddress";
		} catch (Exception e) {
			return "system/500";
		}
	}
	
	/**进入发表评论页面*/
	@RequestMapping(value="/tocomment/{orderid}/{orderItemid}/{goodsid}")
	public String toSendcomment(@PathVariable Long orderid,@PathVariable Long orderItemid,@PathVariable Long goodsid,Model model){
		try {
			Goods goods= goodsService.get(goodsid);
			model.addAttribute("goods", goods);
			model.addAttribute("orderid", orderid);
			model.addAttribute("orderItemid", orderItemid);
			return "center/sendComment";
		} catch (Exception e) {
			logger.error(e);
			return "system/500";
		}
	}
	
	@RequestMapping(value="/savecomment")
	public String savecomment(HttpServletRequest request){
		try { 
			String score = request.getParameter("score");
			String comment = request.getParameter("comment");
			Long goodsid = Long.valueOf(request.getParameter("goodsid"));
			Long orderid = Long.valueOf(request.getParameter("orderid"));
			Long orderItemId = Long.valueOf(request.getParameter("orderItemId"));
			Comment s_comment = new Comment();
			s_comment.setContent(comment);
			s_comment.setMember(new Member(memberid));
			s_comment.setGoods(new Goods(goodsid));
			s_comment.setOrder(new Order(orderid));
			s_comment.setScore(Integer.valueOf(score));
			commentService.saveCommentPc(s_comment, orderItemId);
			return "redirect:/manage/myCenter/mycomment";
		} catch (Exception e) {
			logger.error(e);
			return "system/500";
		}
	}
}
